/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelCliente.java
 *
 * Created on 27/12/2012, 01:52:00
 */
package inovacao.gui;

import inovacao.dao.ClienteDAO;
import inovacao.entidades.Cliente;
import inovacao.modelos.PagingModel;
import java.awt.BorderLayout;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

/**
 *
 * @author Giovani
 */
public final class PanelCliente extends javax.swing.JInternalFrame {

    /** Creates new form PanelCliente */
    private static PanelCliente INSTANCE = null;
    private GridDetalhe jt = null;

    private PanelCliente() {
        super("Clientes");
        addViews();
    }

    public static PanelCliente getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new PanelCliente();
        }
        return INSTANCE;

    }

    public void addViews() {

        PagingModel pm = new PagingModel(new String[]{"CÓDIGO", "NOME", "BAIRRO", "CIDADE", "SALDO DEVEDOR"});
        pm.setPageSize(100);
        pm.setEditavel(false);

        ClienteDAO clienteDAO = new ClienteDAO();
        List<Cliente> clienteDados = clienteDAO.findAll();

        if (clienteDados != null) {
            for (Cliente cliente : clienteDados) {
                pm.addRow(new Object[]{"" + cliente.getIdcliente() + "",
                            "" + cliente.getNome() + "",
                            cliente.getIdbairro(),
                            cliente.getIdcidade(),
                            cliente.getSaldoDevedor()});
            }
        }
        jt = new GridDetalhe(pm);
        jt.validate();

        JScrollPane jsp = PagingModel.createPagingScrollPaneForTable(jt);
        MasterFormCliente masterForm = new MasterFormCliente(jt);
        JScrollPane jsp2 = new JScrollPane(masterForm);
        Navegadores navegadores = new Navegadores(jt, masterForm);
        jt.setNavegadores(navegadores);
        jt.setMasterForm(masterForm);
        jt.selectPrimeiraLinha();
        jt.getColumnModel().getColumn(1).setPreferredWidth(50);

        ///////////////////////////////////////////////////////
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jsp2, jsp);
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation(250);
        //////////////////////////////////////////////////////
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        panel.add(navegadores, BorderLayout.NORTH);
        panel.add(splitPane, BorderLayout.CENTER);
        add(panel);

        final ArrayList<Component> componentes = masterForm.allComponentes();
        for (Component componente : componentes) {
            componente.setEnabled(false);
        }
    }
}
